package com.ruoyi.mine.controller;


import com.ruoyi.common.annotation.DataSource;
import com.ruoyi.common.core.domain.R;
import com.ruoyi.common.enums.DataSourceType;
import com.ruoyi.mine.dto.ProductionInfoUpdateDTO;
import com.ruoyi.mine.dto.ProductionUploadDTO;
import com.ruoyi.mine.pojo.Production;
import com.ruoyi.mine.service.ProductionService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.format.annotation.DateTimeFormat;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.validation.constraints.NotNull;
import java.io.IOException;
import java.time.LocalDate;
import java.util.List;

@Api(tags = "矿山生产数据相关接口", value = "所有接口均已测试通过(包括文件提交)-表中有一字段未使用")
@RequestMapping("/mine/productionData")
@RestController
public class ProductionController {

    @Autowired
    private ProductionService productionService;

    @DataSource(DataSourceType.MASTER)
    @ApiOperation(value = "上传矿山生产数据接口")
    @PostMapping("/upload")
    public R<String> upload(@ModelAttribute @Validated(ProductionUploadDTO.Upload.class) ProductionUploadDTO productionUploadDTO) throws IOException {

        productionService.upload(productionUploadDTO);
        return R.ok("成功上传矿山生产数据");
    }

    @DataSource(DataSourceType.MASTER)
    @ApiOperation(value = "删除矿山生产数据接口")
    @DeleteMapping("/delete")
    public R<String> delete(@RequestParam("id") @NotNull(message = "矿山生产ID不能为空") Integer id) {

        productionService.delete(id);
        return R.ok("矿山生产数据删除成功");
    }

    @DataSource(DataSourceType.MASTER)
    @ApiOperation(value = "更新矿山生产数据接口")
    @PutMapping("/update")
    public R<String> update(@ModelAttribute @Validated(ProductionInfoUpdateDTO.Update.class) ProductionInfoUpdateDTO productionInfoUpdateDTO)
    {

        productionService.update(productionInfoUpdateDTO);
        return R.ok("矿山生产数据更新成功");
    }

    @DataSource(DataSourceType.MASTER)
    @ApiOperation(value = "查询矿山生产数据接口")
    @GetMapping("/search")
    public R<List<Production>> search(@RequestParam(name = "mineId", required = false) String mineId,
                                      @RequestParam(name = "layerName", required = false) String layerName,
                                      @RequestParam(name = "produceType", required = false) String produceType,
                                      @RequestParam(name = "stratTime", required = false) @DateTimeFormat(pattern = "yyyy-MM-dd") LocalDate startTime,
                                      @RequestParam(name = "endTime", required = false) @DateTimeFormat(pattern = "yyyy-MM-dd") LocalDate endTime,
                                      @RequestParam(name = "fileType", required = false) String fileType
                            ) {

        List<Production> productionList = productionService.search(mineId, layerName, produceType, startTime, endTime, fileType);
        return R.ok(productionList, "矿山生产数据查询成功");
    }
}
